/**
 * 
 */
package com.orange.cepappmanager.tools;
import java.util.List;
import java.util.Map;

import com.google.appengine.labs.repackaged.org.json.JSONObject;


/**
 * @author Dida
 *
 */
public class JsonUtils {
	public static String mapToJson(Map<?, ?> map) {
	    try {
	      return (new JSONObject(map)).toString(2);
	    } catch (Exception e) {
	      throw new RuntimeException(e);
	    }
	  }

	  // All code below is only for manually testing this class.

	  /**
	   * Convert an object into its JSON representation.
	   */
	  private static String toJson(Object x) {
	    try {
	      if (x == null || x instanceof String || x instanceof Number || x instanceof Character
	          || x.getClass().isArray() || x instanceof Iterable<?>) {
	        return new JSONObject().put("JSON", x).toString(2);
	      } else if (x instanceof Map<?, ?>) {
	        return (new JSONObject((Map<?, ?>) x)).toString(2);
	      } else if (x instanceof JSONObject) {
	        return ((JSONObject) x).toString(2);
	      } else {
	        return (new JSONObject(x)).toString(2);
	      }
	    } catch (Exception e) {
	      throw new RuntimeException("x=" + x, e);
	    }
	  }

	  /**
	   * Convert a JSON representation into an object
	   */
//	  private static Object fromJson(String json) {
//	    try {
//	      JSONObject jsonObject = new JSONObject(json);
//	      if (jsonObject.has("JSON")) {
//	        return convert(jsonObject.get("JSON"));
//	      } else {
//	        return convert(jsonObject);
//	      }
//	    } catch (Exception e) {
//	      throw new RuntimeException("json=" + json, e);
//	    }
//	  }

	  /**
	   * Convert an <code>org.json.JSONObject</code> into a <code>Map</code> and an
	   * <code>org.json.JSONArray</code> into a <code>List</code>
	   */
//	  private static Object convert(Object x) throws JSONException {
//	    if (x instanceof JSONObject) {
//	      JSONObject jsonObject = (JSONObject) x;
//	      String[] names = JSONObject.getNames(jsonObject);
//	      int numNames = (null == names ? 0 : names.length);
//	      HashMap<String, Object> map = new HashMap<String, Object>(numNames);
//	      if (numNames > 0) {
//	        for (String name : names) {
//	          Object value = jsonObject.get(name);
//	          map.put(name, convert(value));
//	        }
//	      }
//	      return map;
//	    } else if (x instanceof JSONArray) {
//	      JSONArray jsonArray = (JSONArray) x;
//	      int length = jsonArray.length();
//	      List<Object> list = new ArrayList<Object>(length);
//	      for (int i = 0; i < length; i++) {
//	        list.add(convert(jsonArray.get(i)));
//	      }
//	      return list;
//	    } else {
//	      return x;
//	    }
//	  }

	  @SuppressWarnings("unchecked")
	  private static String recursiveToString(Object y) {
	    StringBuilder builder = new StringBuilder(512);
	    if (null == y) {
	      builder.append("null");
	    } else {
	      if (y instanceof List) {
	        List<?> list = (List<?>) y;
	        builder.append("(");
	        boolean first = true;
	        for (Object x : list) {
	          if (!first) {
	            builder.append(", ");
	          }
	          builder.append(recursiveToString(x));
	          first = false;
	        }
	        builder.append(")");
	      } else if (y instanceof Map) {
	        Map<?, ?> map = (Map<?, ?>) y;
	        builder.append("{");
	        boolean first = true;
	        for (Object key : map.keySet()) {
	          if (!first) {
	            builder.append(", ");
	          }
	          builder.append(key);
	          builder.append("=");
	          builder.append(recursiveToString(map.get(key)));
	          first = false;
	        }
	        builder.append("}");
	      } else if (y instanceof String) {
	        builder.append('"');
	        builder.append(y);
	        builder.append('"');
	      } else {
	        builder.append(y);
	      }
	    }
	    return builder.toString();
	  }

//	  private static void debugPrint(Object x) {
//	    System.out.println();
//	    String json = toJson(x);
//	    Object y = fromJson(json);
//	    System.out.println(x + " --> " + json + " --> " + recursiveToString(y));
//	  }


}
